package com.dongdongshop.controller;

import com.dk.data.R;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.apache.zookeeper.Login;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import static com.dk.enums.ResultEnum.PASS_ERROR;
import static com.dk.enums.ResultEnum.USER_NULL;

@Controller
@RequestMapping("user")
public class UserController {


    //登录
    @RequestMapping("shirodeng")
    @ResponseBody
    public R shirodeng(String username, String password){


        Subject subject= SecurityUtils.getSubject();//获取到subject
        UsernamePasswordToken token=new UsernamePasswordToken(username,password);//封装token到LoginRealm类

        try {
            subject.login(token);
        }catch (UnknownAccountException e){
            return R.response(USER_NULL);//用户名不存在
        }catch (IncorrectCredentialsException e){
            return R.response(PASS_ERROR);//密码错误
        }
        return R.ok();//登录成功
    }




}
